package wang.chunfan.springcloud.learning.nacos.user.server.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wang.chunfan.springcloud.learning.consul.user.server.service.UserService;
import wang.chunfan.springcloud.learning.consul.user.server.model.Result;
import wang.chunfan.springcloud.learning.consul.user.server.model.User;

import java.util.List;


/**
 * @version 1.0
 * @Author wangchunfan
 * @Date 2020/5/22 10:00
 **/
@RestController
@RequestMapping("/user")
public class UserController {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @PostMapping
    public Result insert(@RequestBody User user) {
        userService.insert(user);
        LOGGER.info("新增用户：{}", user);
        return new Result();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        userService.delete(id);
        LOGGER.info("根据 id 删除用户，id：{}", id);
        return new Result();
    }

    @PutMapping
    public Result update(@RequestBody User user) {
        userService.update(user);
        LOGGER.info("修改用户：{}", user);
        return new Result();
    }

    @GetMapping("/{id}")
    public Result<User> get(@PathVariable Long id) {
        User user = userService.get(id);
        LOGGER.info("根据 id 获取用户，id：{}", id);
        return new Result(user);
    }

    @GetMapping("/list")
    public Result<List<User>> list(@RequestParam List<Long> ids) {
        List<User> userList = userService.listByIds(ids);
        LOGGER.info("根据 ids 获取用户列表，ids：{}", userList);
        return new Result(userList);
    }

    @GetMapping("/getByUsername/{username}")
    public Result<List<User>> getByUsername(@PathVariable String username) {
        List<User> userList = userService.getByUsername(username);
        LOGGER.info("根据 username 获取用户列表，username：{}", userList);
        return new Result(userList);
    }


}
